//
//  ESCommentCell.m
//  Eyeshot
//
//  Created by admin on 15/7/7.
//  Copyright (c) 2015年 eyeshot. All rights reserved.
//

#define kCommentCellPadding 2 * kHorizen
#define kCommentFont 14
#import "ESCommentCell.h"
#import "ESUserIcon.h"

@interface ESCommentCell ()
@property (nonatomic, weak) ESUserIcon *userIcon;
@property (nonatomic, weak) UILabel *userName;
@property (nonatomic, weak) UILabel *comment;
@end

@implementation ESCommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        ESUserIcon *userIcon = [ESUserIcon buttonWithType:UIButtonTypeCustom];
        userIcon.frame = CGRectMake(kCommentCellPadding, kCommentCellPadding, 8 * kVertical, 8 * kVertical);
        
        UILabel *userName = [[UILabel alloc] init];
        userName.x = kCommentCellPadding + 8 * kVertical + kCommentCellPadding;
        userName.y = kCommentCellPadding;
        userName.width = SCREEN_WIDTH - 3 * kCommentCellPadding - 8 * kVertical;
        userName.height = 4 * kVertical;
        userName.font = [UIFont systemFontOfSize:kCommentFont];
        userName.textAlignment = NSTextAlignmentLeft;
        userName.textColor = [UIColor grayColor];
        
        UILabel *comment = [[UILabel alloc] init];
        comment.x = kCommentCellPadding + 8 * kVertical + kCommentCellPadding;
        comment.y = kCommentCellPadding + 4 * kVertical + kCommentCellPadding;
        comment.width = SCREEN_WIDTH - 3 * kCommentCellPadding - 8 * kVertical;
        comment.height = 2 * kVertical;
        comment.numberOfLines = 0;
        comment.font = [UIFont systemFontOfSize:kCommentFont];
        
//        userIcon.backgroundColor = [UIColor blueColor];
//        userName.backgroundColor = [UIColor greenColor];
//        comment.backgroundColor = [UIColor magentaColor];
        
        [self.contentView addSubview:userIcon];
        [self.contentView addSubview:userName];
        [self.contentView addSubview:comment];
        
        _userIcon = userIcon;
        _userName = userName;
        _comment = comment;
    }
    return self;
}

+ (CGFloat)heightForCommentWithModel:(ESComment *)model {
    NSDictionary *dict = @{NSFontAttributeName:[UIFont systemFontOfSize:kCommentFont]};
    CGFloat commentH = [model.content boundingRectWithSize:CGSizeMake(SCREEN_WIDTH - 3 * kCommentCellPadding - 8 * kVertical, 0) options:NSStringDrawingUsesLineFragmentOrigin attributes:dict context:nil].size.height;
    return commentH;
}

+ (CGFloat)heightForCellWithModel:(ESComment *)model {
    CGFloat commentH = [self heightForCommentWithModel:model];
    CGFloat aH = kCommentCellPadding + 4 * kVertical + kCommentCellPadding + commentH + kCommentCellPadding;
    CGFloat bH = kCommentCellPadding + 8 * kVertical + kCommentCellPadding;
    return aH > bH ? aH : bH;
}


- (void)setModel:(ESComment *)model {
    [_userIcon sd_setBackgroundImageWithURL:[NSURL URLWithString:model.user.profile_image] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"default64"]];
    _userName.text = model.user.username;
    
    CGFloat commentH = [ESCommentCell heightForCommentWithModel:model];
    _comment.height = commentH;
    _comment.text = model.content;
}
@end
